package edu.sandau.app.basic.controller;

import edu.sandau.app.basic.domain.Comment;
import edu.sandau.app.basic.domain.Order;
import edu.sandau.app.basic.domain.SysUser;
import edu.sandau.app.basic.domain.DishWithBLOBs;
import edu.sandau.app.basic.domain.OrderDetail;
import edu.sandau.app.basic.golbal.GlobalEnum;
import edu.sandau.app.basic.service.CommentService;
import edu.sandau.app.basic.service.OrderService;
import edu.sandau.app.basic.service.SysUserService;
import edu.sandau.app.basic.service.DishService;
import edu.sandau.app.basic.service.OrderDetailService;
import edu.sandau.app.basic.vo.BasicMsg;
import edu.sandau.app.basic.vo.BasicMsgWithData;
import edu.sandau.app.basic.vo.CreateCommentRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
@Tag(name = "评论管理", description = "评论管理接口")
public class CommentController {
    
    @Autowired
    private CommentService commentService;
    
    @Autowired
    private OrderService orderService;
    
    @Autowired
    private SysUserService sysUserService;
    
    @Autowired
    private DishService dishService;
    
    @Autowired
    private OrderDetailService orderDetailService;
    
    // 获取所有评论
    @GetMapping
    @Operation(summary = "获取所有评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功",
            content = @Content(mediaType = "application/json", 
                examples = {
                    @ExampleObject(name = "查询成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": [
                            {
                              "orderId": 1,
                              "userId": 1,
                              "dishId": 1,
                              "rating": 5,
                              "commentContent": "非常好吃，下次还会点！",
                              "commentImages": "https://example.com/comment1.jpg",
                              "commentStatus": "0"
                            },
                            {
                              "orderId": 2,
                              "userId": 2,
                              "dishId": 1,
                              "rating": 4,
                              "commentContent": "味道不错，推荐！",
                              "commentImages": "",
                              "commentStatus": "0"
                            }
                          ]
                        }"""),
                    @ExampleObject(name = "暂无评论", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": []
                        }"""),
                    @ExampleObject(name = "查询失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "查询失败: 数据库连接异常",
                          "errorCode": "COMMENT_QUERY_FAIL",
                          "data": null
                        }""")
                }))
    })
    public BasicMsgWithData<List<Comment>> getAllComments() {
        BasicMsgWithData<List<Comment>> msg = new BasicMsgWithData<>();
        try {
            List<Comment> comments = commentService.getAllComments();
            msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
            msg.setErrorMsg("查询成功");
            msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
            msg.setData(comments);
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_QUERY_FAIL);
            msg.setErrorMsg("查询失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }
    
    // 根据ID获取评论
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功",
            content = @Content(mediaType = "application/json", 
                examples = {
                    @ExampleObject(name = "查询成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": {
                            "commentId": 1,
                            "orderId": 1,
                            "userId": 1,
                            "dishId": 1,
                            "rating": 5,
                            "commentContent": "很好吃",
                            "commentImages": "",
                            "commentStatus": "0"
                          }
                        }"""),
                    @ExampleObject(name = "评论不存在", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论不存在",
                          "errorCode": "COMMENT_NOT_FOUND",
                          "data": null
                        }""")
                }))
    })
    public BasicMsgWithData<Comment> getCommentById(@PathVariable Long id) {
        BasicMsgWithData<Comment> msg = new BasicMsgWithData<>();
        try {
            Comment comment = commentService.getCommentById(id);
            if (comment != null) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("查询成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
                msg.setData(comment);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_NOT_FOUND);
                msg.setErrorMsg("评论不存在");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_QUERY_FAIL);
            msg.setErrorMsg("查询失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }
    
    // 直接对菜品评论（不需要订单）
    @PostMapping("/dish")
    @Operation(summary = "直接对菜品评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作完成",
            content = @Content(mediaType = "application/json",
                examples = {
                    @ExampleObject(name = "评论成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "评论发表成功",
                          "errorCode": "Ok"
                        }"""),
                    @ExampleObject(name = "菜品ID不能为空", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "菜品ID不能为空",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "用户ID不能为空", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "用户ID不能为空",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }""")
                }))
    })
    public BasicMsg addDishComment(@RequestBody CreateCommentRequest request) {
        BasicMsg msg = new BasicMsg();
        try {
            StringBuilder errorMsg = new StringBuilder();
            
            // 验证必需字段
            if (request.getDishId() == null) {
                errorMsg.append("菜品ID不能为空; ");
            }
            if (request.getUserId() == null) {
                errorMsg.append("用户ID不能为空; ");
            }
            if (request.getCommentContent() == null || request.getCommentContent().trim().isEmpty()) {
                errorMsg.append("评论内容不能为空; ");
            }
            if (request.getRating() == null || request.getRating() < 0 || request.getRating() > 10) {
                errorMsg.append("评分必须在0-10之间; ");
            }
            if (request.getCommentStatus() != null && !request.getCommentStatus().equals("0") && !request.getCommentStatus().equals("1")) {
                errorMsg.append("评论状态只能是0或1; ");
            }
            
            // 验证菜品存在性
            if (request.getDishId() != null) {
                DishWithBLOBs dish = dishService.getDishById(request.getDishId());
                if (dish == null) {
                    errorMsg.append("菜品不存在; ");
                }
            }
            
            // 验证用户存在性
            if (request.getUserId() != null) {
                SysUser user = sysUserService.getUserById(request.getUserId());
                if (user == null) {
                    errorMsg.append("用户不存在; ");
                }
            }
            
            // 如果有验证错误，返回所有错误
            if (errorMsg.length() > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
                msg.setErrorMsg(errorMsg.toString().trim().replaceAll("; $", ""));
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                return msg;
            }
            
            // 创建Comment对象
            Comment comment = new Comment();
            comment.setOrderId(null); // 直接评论不需要订单ID
            comment.setUserId(request.getUserId());
            comment.setDishId(request.getDishId());
            comment.setRating(request.getRating());
            comment.setCommentImages(request.getCommentImages());
            comment.setCommentStatus(request.getCommentStatus() != null ? request.getCommentStatus() : "0");
            comment.setCommentContent(request.getCommentContent());
            
            // 系统字段由服务层设置
            comment.setCommentId(null);
            comment.setCreateTime(null);
            comment.setUpdateTime(null);
            
            int result = commentService.addComment(comment);
            if (result > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("评论发表成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
                msg.setErrorMsg("评论发表失败");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
            msg.setErrorMsg("评论发表失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }

    // 创建评论（需要订单）
    @PostMapping
    @Operation(summary = "创建评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作完成",
            content = @Content(mediaType = "application/json",
                examples = {
                    @ExampleObject(name = "创建成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "评论创建成功",
                          "errorCode": "Ok"
                        }"""),
                    @ExampleObject(name = "订单ID不能为空", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "订单ID不能为空",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "评论内容不能为空", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论内容不能为空",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "评分范围错误", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评分必须在0-10之间",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "评论状态错误", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论状态只能是0或1",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "订单不存在", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "订单不存在",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "订单不存在", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "订单不存在",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "订单没有详情", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "订单没有详情信息",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "多个验证失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "订单ID不能为空; 评论内容不能为空; 评分必须在0-10之间; 评论状态只能是0或1",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }"""),
                    @ExampleObject(name = "创建失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论创建失败: 具体错误信息",
                          "errorCode": "COMMENT_ADD_FAIL"
                        }""")
                }))
    })
    public BasicMsg addComment(@RequestBody CreateCommentRequest request) {
        BasicMsg msg = new BasicMsg();
        try {
            StringBuilder errorMsg = new StringBuilder();
            
            // 验证必需字段
            if (request.getOrderId() == null) {
                errorMsg.append("订单ID不能为空; ");
            }
            if (request.getCommentContent() == null || request.getCommentContent().trim().isEmpty()) {
                errorMsg.append("评论内容不能为空; ");
            }
            if (request.getRating() == null || request.getRating() < 0 || request.getRating() > 10) {
                errorMsg.append("评分必须在0-10之间; ");
            }
            if (request.getCommentStatus() != null && !request.getCommentStatus().equals("0") && !request.getCommentStatus().equals("1")) {
                errorMsg.append("评论状态只能是0或1; ");
            }
            
            // 验证订单存在性并获取订单信息
            Order order = null;
            if (request.getOrderId() != null) {
                order = orderService.getOrderById(request.getOrderId());
                if (order == null) {
                    errorMsg.append("订单不存在; ");
                }
            }
            
            // 验证订单详情存在性
            OrderDetail orderDetail = null;
            if (request.getOrderId() != null && order != null) {
                List<OrderDetail> orderDetails = orderDetailService.getOrderDetailsByOrderId(request.getOrderId());
                if (orderDetails == null || orderDetails.isEmpty()) {
                    errorMsg.append("订单没有详情信息; ");
                } else {
                    orderDetail = orderDetails.get(0); // 使用第一个订单详情
                }
            }
            
            // 如果有验证错误，返回所有错误
            if (errorMsg.length() > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
                msg.setErrorMsg(errorMsg.toString().trim().replaceAll("; $", ""));
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                return msg;
            }
            
            // 创建Comment对象，自动填充用户ID和菜品ID
            Comment comment = new Comment();
            comment.setOrderId(request.getOrderId());
            comment.setUserId(order.getUserId()); // 从订单表获取用户ID
            comment.setDishId(orderDetail.getDishId()); // 从订单详情表获取菜品ID
            comment.setRating(request.getRating());
            comment.setCommentImages(request.getCommentImages());
            comment.setCommentStatus(request.getCommentStatus() != null ? request.getCommentStatus() : "0");
            comment.setCommentContent(request.getCommentContent());
            
            // 系统字段由服务层设置，不需要客户端传递
            comment.setCommentId(null);
            comment.setCreateTime(null);
            comment.setUpdateTime(null);
            
            int result = commentService.addComment(comment);
            if (result > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("评论创建成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
                msg.setErrorMsg("评论创建失败");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
            msg.setErrorMsg("评论创建失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }
    
    // 删除评论
    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作完成",
            content = @Content(mediaType = "application/json",
                examples = {
                    @ExampleObject(name = "删除成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "评论删除成功",
                          "errorCode": "Ok"
                        }"""),
                    @ExampleObject(name = "删除失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论删除失败",
                          "errorCode": "COMMENT_DELETE_FAIL"
                        }"""),
                    @ExampleObject(name = "系统错误", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "评论删除失败: 具体错误信息",
                          "errorCode": "COMMENT_DELETE_FAIL"
                        }""")
                }))
    })
    public BasicMsg deleteComment(@PathVariable Long id) {
        BasicMsg msg = new BasicMsg();
        try {
            int result = commentService.deleteComment(id);
            if (result > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("评论删除成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_DELETE_FAIL);
                msg.setErrorMsg("评论删除失败");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_DELETE_FAIL);
            msg.setErrorMsg("评论删除失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }
    
    // 根据菜品ID获取评论
    @GetMapping("/dish/{dishId}")
    @Operation(summary = "根据菜品ID获取评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功",
            content = @Content(mediaType = "application/json", 
                examples = {
                    @ExampleObject(name = "查询成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": [
                            {
                              "orderId": 1,
                              "userId": 1,
                              "dishId": 1,
                              "rating": 5,
                              "commentContent": "非常好吃，下次还会点！",
                              "commentImages": "https://example.com/comment1.jpg",
                              "commentStatus": "0"
                            }
                          ]
                        }"""),
                    @ExampleObject(name = "该菜品暂无评论", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "该菜品暂无评论",
                          "errorCode": "COMMENT_NOT_FOUND",
                          "data": null
                        }"""),
                    @ExampleObject(name = "查询失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "查询失败: 数据库连接异常",
                          "errorCode": "COMMENT_QUERY_FAIL",
                          "data": null
                        }""")
                }))
    })
    public BasicMsgWithData<List<Comment>> getCommentsByDishId(@PathVariable Long dishId) {
        BasicMsgWithData<List<Comment>> msg = new BasicMsgWithData<>();
        try {
            List<Comment> comments = commentService.getCommentsByDishId(dishId);
            if (comments != null && !comments.isEmpty()) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("查询成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
                msg.setData(comments);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_NOT_FOUND);
                msg.setErrorMsg("该菜品暂无评论");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                msg.setData(null);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_QUERY_FAIL);
            msg.setErrorMsg("查询失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            msg.setData(null);
        }
        return msg;
    }
    
    // 根据用户ID获取评论
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据用户ID获取评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功",
            content = @Content(mediaType = "application/json", 
                examples = {
                    @ExampleObject(name = "查询成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": [
                            {
                              "orderId": 1,
                              "userId": 1,
                              "dishId": 1,
                              "rating": 5,
                              "commentContent": "非常好吃，下次还会点！",
                              "commentImages": "https://example.com/comment1.jpg",
                              "commentStatus": "0"
                            }
                          ]
                        }"""),
                    @ExampleObject(name = "该用户暂无评论", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "该用户暂无评论",
                          "errorCode": "COMMENT_NOT_FOUND",
                          "data": null
                        }"""),
                    @ExampleObject(name = "查询失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "查询失败: 数据库连接异常",
                          "errorCode": "COMMENT_QUERY_FAIL",
                          "data": null
                        }""")
                }))
    })
    public BasicMsgWithData<List<Comment>> getCommentsByUserId(@PathVariable Long userId) {
        BasicMsgWithData<List<Comment>> msg = new BasicMsgWithData<>();
        try {
            List<Comment> comments = commentService.getCommentsByUserId(userId);
            if (comments != null && !comments.isEmpty()) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("查询成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
                msg.setData(comments);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_NOT_FOUND);
                msg.setErrorMsg("该用户暂无评论");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                msg.setData(null);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_QUERY_FAIL);
            msg.setErrorMsg("查询失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            msg.setData(null);
        }
        return msg;
    }
    
    // 根据订单ID获取评论
    @GetMapping("/order/{orderId}")
    @Operation(summary = "根据订单ID获取评论")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功",
            content = @Content(mediaType = "application/json", 
                examples = {
                    @ExampleObject(name = "查询成功", value = """
                        {
                          "operateResult": "SUCC",
                          "errorMsg": "查询成功",
                          "errorCode": "Ok",
                          "data": [
                            {
                              "orderId": 1,
                              "userId": 1,
                              "dishId": 1,
                              "rating": 5,
                              "commentContent": "非常好吃，下次还会点！",
                              "commentImages": "https://example.com/comment1.jpg",
                              "commentStatus": "0"
                            }
                          ]
                        }"""),
                    @ExampleObject(name = "该订单暂无评论", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "该订单暂无评论",
                          "errorCode": "COMMENT_NOT_FOUND",
                          "data": null
                        }"""),
                    @ExampleObject(name = "查询失败", value = """
                        {
                          "operateResult": "UNSUSS",
                          "errorMsg": "查询失败: 数据库连接异常",
                          "errorCode": "COMMENT_QUERY_FAIL",
                          "data": null
                        }""")
                }))
    })
    public BasicMsgWithData<List<Comment>> getCommentsByOrderId(@PathVariable Long orderId) {
        BasicMsgWithData<List<Comment>> msg = new BasicMsgWithData<>();
        try {
            List<Comment> comments = commentService.getCommentsByOrderId(orderId);
            if (comments != null && !comments.isEmpty()) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("查询成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
                msg.setData(comments);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_NOT_FOUND);
                msg.setErrorMsg("该订单暂无评论");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                msg.setData(null);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_QUERY_FAIL);
            msg.setErrorMsg("查询失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            msg.setData(null);
        }
        return msg;
    }
    
    // 切换评论状态
    @PostMapping("/toggle-status/{id}")
    @Operation(summary = "切换评论状态")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "操作成功"),
        @ApiResponse(responseCode = "400", description = "参数错误"),
        @ApiResponse(responseCode = "404", description = "评论不存在")
    })
    public BasicMsg toggleCommentStatus(@PathVariable Long id, @RequestParam String status) {
        BasicMsg msg = new BasicMsg();
        try {
            // 验证状态参数
            if (!status.equals("0") && !status.equals("1")) {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
                msg.setErrorMsg("状态参数错误，只能是0（显示）或1（隐藏）");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
                return msg;
            }
            
            int result = commentService.toggleCommentStatus(id, status);
            if (result > 0) {
                msg.setErrorCode(GlobalEnum.ErrorCode.Ok);
                msg.setErrorMsg("状态切换成功");
                msg.setOperateResult(GlobalEnum.OperateResult.SUCC);
            } else {
                msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_NOT_FOUND);
                msg.setErrorMsg("评论不存在或更新失败");
                msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
            }
        } catch (Exception e) {
            msg.setErrorCode(GlobalEnum.ErrorCode.COMMENT_ADD_FAIL);
            msg.setErrorMsg("状态切换失败: " + e.getMessage());
            msg.setOperateResult(GlobalEnum.OperateResult.UNSUSS);
        }
        return msg;
    }
}
